﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.GERP.GFMS.APP.App_UI;

namespace KPIS.GERP.GFMS.APP.App_UI.UserControls
{
    public partial class Toolbar : System.Web.UI.UserControl
    {
        protected Panel parentPanelGrid;
        protected Panel parentPanelEdit;
        public delegate void ParentInsert();
        public ParentInsert ParentCmdInsert;
        public delegate void ParentUpdate();
        public ParentUpdate ParentCmdUpdate;
        public delegate void ParentDelete();
        public ParentDelete ParentCmdDelete;
        public delegate void ParentAddNew();
        public ParentAddNew ParentCmdAddNew;
        public delegate void ParentEdit();
        public ParentEdit ParentCmdEdit;

        public enum ToolbarMode
        {
            ViewAll,
            ViewData,
            AddNew,
            Edit
        };

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        public Panel ParentPanelGrid
		{
			get
			{ 
				return parentPanelGrid;
			}
			set 
			{
				parentPanelGrid = value;
			}
		}

        public Panel ParentPanelEdit
		{
			get
			{ 
				return parentPanelEdit;
			}
			set 
			{
				parentPanelEdit = value;
			}
		}

        public void UpdateToolbar(ToolbarMode Mode, bool bReport)
        {
            switch(Mode)
            {
                case ToolbarMode.ViewAll :
                    lblMode.Text = "รายการทั้งหมด";

                    cmdViewAll.Visible = false;
                    cmdAddNew.Visible = true;
                    cmdEdit.Visible = true;
                    cmdDelete.Visible = true;
                    cmdInsert.Visible = false;
                    cmdUpdate.Visible = false;

                    if (parentPanelGrid != null)
                        parentPanelGrid.Visible = true;
                    if (parentPanelEdit != null)
                        parentPanelEdit.Visible = false;
                    break;
                case ToolbarMode.ViewData :
                    break;
                case ToolbarMode.AddNew :
                    lblMode.Text = "เพิ่มรายการใหม่";

                    cmdViewAll.Visible = true;
                    cmdAddNew.Visible = false;
                    cmdEdit.Visible = false;
                    cmdDelete.Visible = false;
                    cmdInsert.Visible = true;
                    cmdUpdate.Visible = false;

                    if (parentPanelGrid != null)
                        parentPanelGrid.Visible = false;
                    if (parentPanelEdit != null)
                        parentPanelEdit.Visible = true;
                    break;
                case ToolbarMode.Edit :
                    lblMode.Text = "แก้ไขรายการ";

                    cmdViewAll.Visible = true;
                    cmdAddNew.Visible = false;
                    cmdEdit.Visible = false;
                    cmdDelete.Visible = false;
                    cmdInsert.Visible = false;
                    cmdUpdate.Visible = true;

                    if (parentPanelGrid != null)
                        parentPanelGrid.Visible = false;
                    if (parentPanelEdit != null)
                        parentPanelEdit.Visible = true;
                    break;
            }

            panelReport.Visible = bReport;
        }

        protected void cmdViewAll_Click(object sender, EventArgs e)
        {
            UpdateToolbar(ToolbarMode.ViewAll, panelReport.Visible);
        }

        protected void cmdAddNew_Click(object sender, EventArgs e)
        {
            if (ParentCmdAddNew != null)
                ParentCmdAddNew();
            UpdateToolbar(ToolbarMode.AddNew, panelReport.Visible);
        }

        protected void cmdEdit_Click(object sender, EventArgs e)
        {
            if (ParentCmdEdit != null)
				ParentCmdEdit();
            UpdateToolbar(ToolbarMode.Edit, panelReport.Visible);
        }

        protected void cmdDelete_Click(object sender, EventArgs e)
        {
            if (ParentCmdDelete != null)
				ParentCmdDelete();
            //UpdateToolbar(ToolbarMode.ViewAll, panelReport.Visible);
        }

        protected void cmdInsert_Click(object sender, EventArgs e)
        {
            if (ParentCmdInsert != null)
				ParentCmdInsert();
            UpdateToolbar(ToolbarMode.ViewAll, panelReport.Visible);
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            if (ParentCmdUpdate != null)
				ParentCmdUpdate();
            UpdateToolbar(ToolbarMode.ViewAll, panelReport.Visible);
        }

        protected void cmdReport_Click(object sender, EventArgs e)
        {
            
            //UpdateToolbar(ToolbarMode.ViewAll, panelReport.Visible);
        }

        public ToolbarMode GetToolbarMode()
        {
            ToolbarMode mode = ToolbarMode.AddNew;

            switch (lblMode.Text)
            {
                case "รายการทั้งหมด":
                    mode = ToolbarMode.ViewAll;
                    break;
                case "เพิ่มรายการใหม่":
                    mode = ToolbarMode.AddNew;
                    break;
                case "แก้ไขรายการ":
                    mode = ToolbarMode.Edit;
                    break;
            }

            return mode;
        }
    }
}